package com.egg.ylt.presenter.impl;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.egg.ylt.R;
import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.activity.ACT_BigSinglePic;
import com.egg.ylt.activity.ACT_EditBabyInfo;
import com.egg.ylt.activity.ACT_GrowUpRecord;
import com.egg.ylt.activity.ACT_MultiBigPhotos;
import com.egg.ylt.activity.ACT_SelectBaby;
import com.egg.ylt.activity.ACT_SwitchBaby;
import com.egg.ylt.activity.ACT_UserLogin;
import com.egg.ylt.pojo.BabyDynamicMsgEntity;
import com.egg.ylt.pojo.CurrentBabyInfoEntity;
import com.egg.ylt.pojo.MemberBabyEntity;
import com.egg.ylt.pojo.RelationEntity;
import com.egg.ylt.pojo.UploadPhotoEntity;
import com.egg.ylt.view.IRecordView;
import com.lzy.imagepicker.ImagePicker;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.io.File;
import java.util.HashMap;

/**
 * Created By Dlx
 * on 2019/1/18
 */
public class RecordPresenter extends BasePresenter<IRecordView> {

    public void getCurrentBaby(String token, String companyId, String babyId) {
        HashMap<String, String> parms = new HashMap<>();
        parms.put("token", token);
        parms.put("companyId", companyId);
        parms.put("babyId", babyId);
        RequestManager.getInstance().requestPostByAsyn(API.GET_CURRENT_BABY_INFO, parms, new ReqCallBack<CurrentBabyInfoEntity>() {

            @Override
            public void onReqSuccess(CurrentBabyInfoEntity result) {
                mView.onCurrentBabyReceived(result);
            }

            @Override
            public void onFailure(String result) {
                mView.onCurrentBabyNotFound();
                mView.showToast(result);

            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.onCurrentBabyNotFound();

            }
        });
    }

    public Context getContext() {
        return mContext;
    }

    /**
     * @param dataId      待删除数据ID
     * @param requestType 0 为未确认请求，需要确认  1为已确认请求，执行删除
     */
    public void deleteBabyDynamicMsg(String dataId, int requestType, final int position) {
        if (requestType == 0) {
            mView.showDeleteConfirmDialog(dataId, position);
            return;
        } else if (requestType == 1) {
//            mView.showLoading(mContext.getString(R.string.data_loading));
            HashMap<String, String> parms = new HashMap<>();
            parms.put("token", Constants.TOKEN);
            parms.put("companyId", Constants.COMPANYID);
            parms.put("id", dataId);
            //1/26新加字段，要求只有管理员能删改宝宝动态信息
            parms.put("babyId", Constants.BABYID);
            RequestManager.getInstance().requestGetByAsyn(API.DELETE_BABY_DYNAMIC_MSG, parms, new ReqCallBack<Object>() {

                @Override
                public void onReqSuccess(Object result) {
                    mView.dismissDialogLoading();
                    mView.onDeleteSuccess(position);
                }

                @Override
                public void onFailure(String result) {
                    mView.dismissDialogLoading();
                    mView.showToast(result);
                }

                @Override
                public void onReqFailed(ErrorBean error) {
                    mView.dismissDialogLoading();
                    mView.showToast(error.getMsg());
                }
            });
        }
    }

    /**
     * 健康记录页，item 分享预留方法，分享单条动态在此实现
     *
     * @param dataId
     * @param position
     */
    public void shareDynamic(String dataId, int requestType, int position) {

    }

    public void choosePicType() {
        mView.showChoosePicDialog();
    }

    public void saveDiaryPhoto(String token, String companyId, String imageUrl, String thumbnailUrl, String babyId) {
//        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("companyId", companyId);
        hashMap.put("imageUrl", imageUrl);
        hashMap.put("thumbnailUrl", thumbnailUrl);
        hashMap.put("babyId", babyId);
        RequestManager.getInstance().requestPostByAsyn(API.POST_SAVE_DIARY_PHOTO, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.onSaveDiarySucceed();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showToast(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showToast(error.getMsg());
            }
        });
    }

    public void upLoadPhoto(String imgUrl) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, Object> hashMap = new HashMap<>();
        File photoFile = new File(imgUrl);
        hashMap.put("file", photoFile);
        RequestManager.getInstance().upLoadFile(API.POST_UPLOAD_PHOTO, hashMap, new ReqCallBack<UploadPhotoEntity>() {

            @Override
            public void onReqSuccess(UploadPhotoEntity result) {
                mView.dismissDialogLoading();
                mView.onUploadSucceed(result);
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showToast(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showToast(error.getMsg());
            }
        });
    }


    public void getBabyDynamicMsgs(String token, String companyId, String babyId, String pageSize, String pageNum) {
//        mView.showLoading(mContext.getString(R.string.data_loading));
        HashMap<String, String> parms = new HashMap<>();
        parms.put("token", token);
        parms.put("companyId", companyId);
        parms.put("babyId", babyId);
        parms.put("pageSize", pageSize);
        parms.put("pageNum", pageNum);
        RequestManager.getInstance().requestPostByAsyn(API.GET_BABY_DYNAMIC_MSGS, parms, new ReqCallBack<BabyDynamicMsgEntity>() {

            @Override
            public void onReqSuccess(BabyDynamicMsgEntity result) {
                mView.hideLoading();
                mView.onBabyMsgsReceived(result);
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showToast(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showToast(error.getMsg());
            }
        });
    }

    /**
     *
     */
    public boolean isLogin() {

        return !"".equals(Constants.TOKEN);
    }

    public void jumpToLogin() {
        mContext.startActivity(new Intent(mContext, ACT_UserLogin.class));
    }

    public void jumpToAddBaby() {
        //type the jump code here
        ACT_SelectBaby.startActResource(mContext, ACT_EditBabyInfo.START_FROM_HEALTH);
    }

    public void editBabyMsg(CurrentBabyInfoEntity entity) {
        Intent intent = new Intent(mContext, ACT_EditBabyInfo.class);
        MemberBabyEntity.ListBean listBean = new MemberBabyEntity.ListBean();
        listBean.setBabyId(entity.getBabyId());
        listBean.setBirthDay(entity.getDate());
        listBean.setGender(entity.getGender());
        listBean.setHeight(entity.getHeight());
        listBean.setWeight(entity.getWeight());
        listBean.setImageUrl(entity.getImageUrl());
        listBean.setName(entity.getName());
        listBean.setRelationshipId(entity.getRelationId());
        listBean.setRelationshipName(entity.getRelationName());
        intent.putExtra("bean", listBean);
        intent.putExtra("sex", entity.getGender());
        intent.putExtra(ACT_SelectBaby.KEY_RESOURCE_TYPE, ACT_EditBabyInfo.START_FROM_HEALTH);
        mContext.startActivity(intent);
    }

    public void jumpToBigPic(String imgUrl) {
        mContext.startActivity(new Intent(mContext, ACT_BigSinglePic.class).putExtra("big_single_pic", imgUrl));
    }

    public void jumpToMultiPics(int mCurrentPosition) {
        mContext.startActivity(new Intent(mContext, ACT_MultiBigPhotos.class)
                .putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, mCurrentPosition)
                .putExtra(Constants.ENTER_TYPE, ACT_MultiBigPhotos.RECORD_ENTERTYPE));
    }

    public void jumpToGrowUpRecord(String babyId) {
        Intent intent = new Intent(mContext, ACT_GrowUpRecord.class);
        Bundle bundle = new Bundle();
        bundle.putString(ACT_GrowUpRecord.KEY_BABY_ID, babyId);
        intent.putExtras(bundle);
        mContext.startActivity(intent);
    }

    public void addRelative(String babyId, String relationshipId, String relationshipName) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("babyId", babyId);
        hashMap.put("relationId", relationshipId);
//        hashMap.put("relationshipName", relationshipName);
        RequestManager.getInstance().requestPostByAsyn(API.ADD_BABY_USER_RELATIONSHIP, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.bindRelativeSuccess();
            }

            @Override
            public void onFailure(String result) {
//                mView.showError(result);
                mView.showToast(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
//                mView.showException(error.toString());
                mView.showToast(error.getMsg());
            }
        });
    }

    public void getRelationList() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        RequestManager.getInstance().requestGetByAsyn(API.GET_RELATION_LIST, hashMap, new ReqCallBack<RelationEntity>() {
            @Override
            public void onReqSuccess(RelationEntity result) {
                mView.updateRelationList(result.getList());
            }

            @Override
            public void onFailure(String result) {
                mView.showToast(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.showToast(error.getMsg());
            }
        });
    }

}
